/**
 * @param {number[]} nums
 * @return {number[]}
 */
export var findDuplicates = function (nums) {
  const [map, ans] = [{}, []]
  for (const num of nums) {
    map[num] = map[num] ? map[num] + 1 : 1
    if (map[num] === 2) {
      ans.push(num)
    }
  }
  return ans
}

export var findDuplicates2 = function (nums) {
  const n = nums.length
  const ans = []
  for (let i = 0; i < n; ++i) {
    const x = Math.abs(nums[i])
    if (nums[x - 1] > 0) {
      nums[x - 1] = -nums[x - 1]
    } else {
      ans.push(x)
    }
  }
  return ans
}
